#include <QtGui>
#include "room.h"
extern "C"{
#include "message/clientMessage.h"
#include "message/clientInfo.h"
}
extern void (*getRoomsFunc)(void);
RoomDialog* curRoomdia;
void getRooms_global()
{
	curRoomdia->reflushRoom();
}
RoomDialog::RoomDialog(MainDialog *man)
{
    mdia=man;
    createMusicGroupBox();

    okButton = new QPushButton(tr("OK"));
    cancelButton = new QPushButton(tr("Cancel"));
    okButton->setDefault(true);

    connect(buttons[0], SIGNAL(clicked()), this, SLOT(choice()));

	connect(buttons[1], SIGNAL(clicked()), this, SLOT(makeRoom_ui()));
	connect(buttons[2],SIGNAL(clicked()),this,SLOT(reflushRoom_ui()));
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch(1);
    buttonLayout->addWidget(okButton);
    buttonLayout->addWidget(cancelButton);
   
    QVBoxLayout *rightLayout =new QVBoxLayout;
    rightLayout->addWidget(musicGroupBox);
    

    
   
    
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addLayout(rightLayout);
    setLayout(mainLayout);
    resize(600,400);
    setWindowTitle(tr("Radio Star"));
	curRoomdia=this;
	getRoomsFunc=getRooms_global;
}
void RoomDialog::createIdGroupBox(){

    idGroupBox = new QGroupBox(tr("Id"));
    QVBoxLayout *layout =new QVBoxLayout;  
    
    idlistwidget = new QListWidget(this);
   
    layout->addWidget(idlistwidget);
   
    idGroupBox->setLayout(layout);
}

void RoomDialog::createMusicGroupBox()
{
    musicGroupBox = new QGroupBox(tr("Room"));
    QVBoxLayout *layout = new QVBoxLayout;

    buttons[0] = new QPushButton(tr("choice"));
    musiclistwidget = new QListWidget(this);
	buttons[1]=new QPushButton(tr("makeRoom"));
	buttons[2]=new QPushButton(QString::fromUtf8("방목록 새로고침"));
	


    layout->addWidget(musiclistwidget);
    layout->addWidget(buttons[0]);
	layout->addWidget(buttons[1]);
	layout->addWidget(buttons[2]);
    musicGroupBox->setLayout(layout);
}

void RoomDialog::createGridGroupBox()
{
    gridGroupBox = new QGroupBox(tr("chatting"));
    QGridLayout *layout = new QGridLayout;

 
    lineEdits = new QLineEdit;
    layout->addWidget(lineEdits,5,0);
   
   enterButton = new QPushButton(tr("input"));
   layout->addWidget(enterButton,5,1);    
    smallEditor = new QTextEdit;
    layout->addWidget(smallEditor,0,0,4,2);
    gridGroupBox->setLayout(layout);
}
void RoomDialog::input(){
	printf("clicked!!!!!!\n");
}
void RoomDialog::choice()
{
	int selectRow=musiclistwidget->currentRow();
	if(selectRow<0 || selectRow>=roomsNum) return;
	int roomNo=roomsList[selectRow].roomID;
	printf("select no : %d\n",roomNo);
	joinRoom(roomNo);
 mdia->show();
 this->hide();
}

void RoomDialog::makeRoom_ui(){
	printf("clicked\n");
	char roomName[100];
	sprintf(roomName,"테스트-%d",myUserInfo.userID);
	int r=makeRoom(roomName);
	if(r<0) return;
	mdia->show();
	this->hide();
}
void RoomDialog::reflushRoom(){
	printf("execute\n");
    musiclistwidget->clear();
   	int i;
	for(i=0;i<roomsNum;i++){
		const QString name=QString::fromUtf8(roomsList[i].name);
		musiclistwidget->insertItem(i,name);
	}
}
void RoomDialog::reflushRoom_ui(){
	reflushRoom();
}

